<template>
  <div>
    <van-nav-bar title="录入手机号" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="form.telPhone"
        name="phone"
        label="手机号"
        placeholder="手机号"
        :rules="[{ required: true,pattern, message: '请填写手机号' }]"
      />
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit">提交</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import {editPhone} from '@/api/user.js'
export default {
  components: {},
  data() {
    return {
      form: {},
      pattern: /^[1]([3-9])[0-9]{9}$/
    }
  },
  created() {
    // let _href = window.location.href // 获取当前地址
    // if (_href.indexOf('?replace=1') <= -1) {
    //   // 若没有replace参数，则添加进去
    //   window.location.replace(_href + '?replace=1')
    // }
  },
  methods: {
    onSubmit(values) {
      // var form
      this.form.telPhone = values.phone
      this.form.id = JSON.parse(localStorage.getItem('user')).id
      console.log(this.form, '获取手机号参数', JSON.parse(localStorage.getItem('user')))
      editPhone(this.form).then(res => {
        console.log(res, 'submit', res, localStorage.getItem('user'))
        let user = JSON.parse(localStorage.getItem('user'))
        user.telPhone = res.data.telPhone
        localStorage.setItem('user', JSON.stringify(user))
        console.log(JSON.parse(localStorage.getItem('user')), 'xin', user)
        this.$router.replace({
          name: 'home'
        })
      })
    },
    onClickLeft() {
      this.$router.go(-1)
    }
  }
}
</script>

<style lang="scss" scoped>
.sub {
  position: fixed;
  bottom: 0;
  width: 100%;
  padding: 10px 20px;
  border-top: 1px solid #eee;
  box-sizing: border-box;
  background-color: #fff;
  .sub_title {
    background-color: rgb(195, 0, 51);
    height: 40px;
    text-align: center;
    color: #fff;
    font-size: 16px;
    line-height: 40px;
  }
}
</style>
